package com.zhuchaooasystem.presenter.home.serviceradect;

import android.app.Activity;

import com.zhuchaooasystem.api.HttpFactory;
import com.zhuchaooasystem.api.RxResultHelper;
import com.zhuchaooasystem.base.BasePresenter;
import com.zhuchaooasystem.base.SchedulersCompat;
import com.zhuchaooasystem.entity.ServicePeopleEntity;
import com.zhuchaooasystem.entity.ServiceRedactAlterEntity;

import java.util.HashMap;
import java.util.Map;

import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by zhangyajiang on 2016/12/22.
 */

public class ServiceRadectPresenter extends BasePresenter<ServiceRadectContract.View>
        implements ServiceRadectContract.Presenter {

    public ServiceRadectPresenter(Activity activity, ServiceRadectContract.View view) {
        super(activity, view);
    }

    /**
     * 获取服务人
     *
     * @param id
     */
    @Override
    public void downServicePeople(String id) {
        Map<String, String> map = new HashMap<>();
        map.put("serviceid", id);


        Subscription subscription = HttpFactory.getHttpApi().getServicePeople(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                        ServicePeopleEntity entity = (ServicePeopleEntity) o;

                        if (entity != null) {
                            mView.showServicePeople(entity);
                        }


                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {


                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }

    /**
     * 提交编辑
     * serviceid=服务单号
     * nonce_str=预约的时间，格式 2016/12/24 16:21:11
     * ContentMesssage=提交的用户ID,用 英文状态下的,做识别拼接成字符串，"4,208"
     * memberId=4用户编号
     * UserName=用户名称
     */
    @Override
    public void alterServiceRedact(String serviceid, String nonce_str,
                                   String ContentMesssage, String memberId, String UserName,
                                   String FlowRemark, String FormatCode) {
        Map<String, String> map = new HashMap<>();
        map.put("serviceid", serviceid);
        map.put("nonce_str", nonce_str);
        map.put("ContentMesssage", ContentMesssage);
        map.put("memberId", memberId);
        map.put("UserName", UserName);
        map.put("FlowRemark", FlowRemark);
        map.put("FormatCode", FormatCode);

        Subscription subscription = HttpFactory.getHttpApi().getAlterResult(map)
                .compose(SchedulersCompat.applyIoSchedulers())
                .compose(RxResultHelper.handleResult())
                .subscribe(new Action1<Object>() {
                    @Override
                    public void call(Object o) {

                        ServiceRedactAlterEntity entity = (ServiceRedactAlterEntity) o;

                        if (entity != null) {
                            mView.showAlterResult(entity);
                        }


                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                        handleError(throwable);
                    }
                });
        addSubscrebe(subscription);
    }
}
